var queryDate = "";

var $table = $("#list_table");

var consultDay = {
    elem: "#consultDay",
    max: 0,
    type: 'month',
    /* Object
     date: 22
     hours: 0
     minutes: 0
     month: 9
     seconds: 0
     year: 2017*/
    done: function (value, date) {
        queryDate = value;
        //选完基准日期后查询
        $table.bootstrapTable("refresh");
    }
};

laydate.render(consultDay);

$(function () {
    var url = urlcore + "/api/muser/getRainbowMemberListViaPeriod";
    initTable($table, url);
});

//筛选是否已发礼品，刷新列表
$("#curIsGifted").change(function () {
    $table.bootstrapTable("refresh");
});

var columns = [
    {
        title: "序号",
        align: "center",
        width: "50px",
        formatter: function (value, row, index) {
            return (index + 1);
        }
    }, {
        field: "id",
        title: "会员id",
        halign: "center",
        align: "center",
        width: "80px",
        // visible: false
    }, {
        field: "userName",
        title: "姓名",
        halign: "center",
        align: "center",
        width: "160px",
    }, {
        field: "phone",
        title: "手机号",
        halign: "center",
        align: "center",
        width: "160px",
    }, {
        field: "sex",
        title: "性别",
        halign: "center",
        align: "center",
        width: "160px",
    }, {
        field: "phone",
        title: "手机号",
        halign: "center",
        align: "center",
        width: "160px",
    }, {
        field: "isGifted",
        title: "是否已发放礼品",
        halign: "center",
        align: "center",
        width: "160px",
        formatter: function (value, row, index) {
            value = value == 0 ? "未发" : "已发";
            return value;
        }
    }, {
        field: "gmtDatetime",
        title: "注册/升级时间",
        halign: "center",
        align: "center",
        width: "150px",
    }, {
        title: "操作",
        halign: "center",
        sortable: true,
        align: "center",
        width: "150px",
        formatter: function (value, row, index) {
            var optHtml = "";
            if (row.isGifted == 0)
                optHtml += "<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"saveEdit(" + row.id + ")\"> 发放礼品</a>\n";
            return optHtml;
        }
    }];

//查询参数
var queryParams = function (params) {
    var search = params.search == undefined ? "" : params.search;
    console.log("params:", params);
    var param = {
        offset: params.offset,
        current: params.offset / params.limit + 1,//bootstrap-table分页参数offset从0开始，计算的后端当前页参数current=(offset/limit+1)，
        size: params.limit,
        condition: {
            searchText: search,
            isGifted: $("#curIsGifted").val(),
            queryDate: queryDate
        }
    };

    return param;
};

//初始化表格，有已有和待选的商品两类表格 tableins:表格实例 ,
// insTag 0表示当前已添加表格 1表示待添加的表格
function initTable(tableins, url) {
    tableins.bootstrapTable({
        columns: columns,
        method: "post",
        url: url,
        dataType: "json",
        queryParams: function (params) {
            return queryParams(params);
        },
        //返回数据预处理
        responseHandler: function (cbdata) {
            console.log(cbdata);
            if (null != cbdata.data && null != cbdata.data.records) {
                var records = cbdata.data.records;
                var res = {
                    //如果采用server端分页，必须有rows和total这两部分数据
                    "rows": records,
                    "total": cbdata.data.total
                };
                return res;
            } else return {"rows": [], "total": 0};
        },
        cache: false,
        // toolbar: '#toolbar',//自定义工具栏，不同表格工具栏不同，一个jQuery 选择器，指明自定义的toolbar 例如:#toolbar, .toolbar.
        pagination: true,//启用分页条
        sidePagination: "server",//分页方式client或server
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        showRefresh: true,//显示刷新
        sortable: true,//是否启用排序
        // clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,//搜索,未设置分页方式为"server"时只是客户端搜索
        // searchText:"",//初始搜索文本
    });
}

//刪除
function doDelete(id) {
    confirm("确认删除吗？", function (callback) {
        if (callback) {
            delCoupon();
        }
    });

    function delCoupon() {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/message/delMsg?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    $table.bootstrapTable("refresh");
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("删除失败");
            }
        });
    }

}

function saveEdit(id) {
    if (undefined == id || "" == id)
        return;
    var id = id;
    var muser = {
        id: id,
        isGifted: 1
    };
    $.ajax({
        type: "POST",
        url: urlcore + "/api/muser/uptMuser",
        dataType: "json",
        contentType: "application/json;charset=UTF-8",
        data: JSON.stringify(muser),
        success: function (data) {
            alert("成功！");
            console.log("data", data);
            $table.bootstrapTable("refresh");
        },
        error: function () {

        }
    });


}